/**
 * @author: tengjuyuan
 * @file: Vector.h
 * 队列的数组实现
 */
#pragma once
#include <iostream>

template <typename T>
class Queue
{
public:
    explicit Queue(int queueCapcaty = 10);  // 创建队列
    ~Queue();                   // 销毁队列        
    void clear();               // 清空队列
    bool isEmpty() const;       // 队列判空
    bool isFull() const;        // 队列判满
    int size() const;           // 队列大小
    bool enQueue(T element);    // 入队
    bool deQueue(T& element);   // 出队
    void traverse();            // 遍历打印队列
    
private:
    int m_nCapacity;            // 队列容量
    int m_nLen;                 // 队列长度
    int m_nHead;                // 队头索引
    int m_nTrail;               // 队尾索引
    T* m_pQueue;                // 队列数组指针
};

template <typename T>
Queue<T>::Queue(int queueCapcaty)
 : m_nCapacity(queueCapcaty)
 , m_nLen(0)
 , m_nHead(0)
 , m_nTrail(0)
 , m_pQueue(NULL)
{
    m_pQueue = new T[m_nCapacity];
}

template <typename T>
Queue<T>::~Queue()
{
    delete [] m_pQueue;
    m_pQueue = NULL;
}

template <typename T>
bool Queue<T>::isEmpty() const
{
    return (0 == size());
}

template <typename T>
int Queue<T>::size() const
{
    return m_nLen;
}

template <typename T>
bool Queue<T>::isFull() const
{
    return (m_nCapacity == size());
}

template <typename T>
bool Queue<T>::enQueue(T element)
{
    if (!isFull())
    {
        m_nLen++;
        m_pQueue[m_nTrail] = element;
        m_nTrail++;
        m_nTrail = m_nTrail % m_nCapacity;
        return true;
    }
    
    return false;
}

template <typename T>
bool Queue<T>::deQueue(T& elememt)
{
    if (isEmpty())
    {
        return false;
    }
    else
    {
        elememt = m_pQueue[m_nHead];
        m_nHead++;
        m_nHead = m_nHead % m_nCapacity;
        m_nLen--;
        return true;
    }
    
}

template <typename T>
void Queue<T>::traverse()
{
	for (int i = m_nHead;  i < m_nHead + m_nLen; i++)
	{
		std::cout << m_pQueue[i % m_nLen] << std::endl;
	}
}
